/** @file    EncodersXmlQt_Xtended.cpp
 *  @author  Alessandro Polo
 *  @version $Id: EncodersXmlQt_Xtended.cpp 2804 2010-07-27 04:39:07Z alex $
 *  @brief
 * The header for this class can be found in EncodersXmlQt_Xtended.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/persistence/qt/EncodersXmlQt_Xtended.h>

 #include <core/ObjectAllocator.h>


namespace wosh {
 namespace persistence {
  namespace qt {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Xtended_ManItem, "wosh::xtended::ManItem", 0.0, _static_EncoderXml_Qt_Xtended_ManItem )

WRESULT EncoderXml_Qt_Xtended_ManItem::serializeTo( const wosh::xtended::ManItem* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	EncoderBaseXmlQt::serializeString( "name", object->getName(), doc, contNode, true );
	EncoderBaseXmlQt::serializeString( "description", object->getDescription(), doc, contNode, encoder_options );
	return WRET_OK;
 }

WRESULT EncoderXml_Qt_Xtended_ManItem::deserializeFrom( wosh::xtended::ManItem* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	object->setName( EncoderBaseXmlQt::deserializeString( "name", "", contNode ) );
	object->setDescription( EncoderBaseXmlQt::deserializeString( "description", "", contNode ) );
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Xtended_WeatherInfo, "wosh::xtended::WeatherInfo", 0.0, _static_EncoderXml_Qt_Xtended_WeatherInfo )

WRESULT EncoderXml_Qt_Xtended_WeatherInfo::serializeTo( const wosh::xtended::WeatherInfo* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	EncoderBaseXmlQt::serializeLong( "timestamp", object->getTimeStamp(), doc, contNode, encoder_options );
	QDomElement periodNode = doc.createElement("period"); contNode.appendChild(periodNode);
	periodNode.setAttribute("name", QString( object->getPeriodName().c_str() ) );
	if ( object->getPeriodStart() != 0 )
		EncoderBaseXmlQt::serializeLong( "start", object->getPeriodStart(), doc, periodNode, encoder_options );
	if ( object->getPeriodEnd() != 0 )
		EncoderBaseXmlQt::serializeLong( "end", object->getPeriodEnd(), doc, periodNode, encoder_options );
	QDomElement conditionNode = doc.createElement("condition"); contNode.appendChild(conditionNode);
	conditionNode.setAttribute("key", QString( object->getConditionKey().c_str() ) );
	if ( !object->getConditionSummary().empty() )
		EncoderBaseXmlQt::serializeString( "summary", object->getConditionSummary(), doc, conditionNode, encoder_options );
	QDomElement temperatureNode = doc.createElement("temperature"); contNode.appendChild(temperatureNode);
	if ( object->getTemperatureHighest() != _WeatherInfo_TemperatureUndefined )
		EncoderBaseXmlQt::serializeLong( "highest", object->getTemperatureHighest(), doc, temperatureNode, encoder_options );
	if ( object->getTemperatureLowest() != _WeatherInfo_TemperatureUndefined )
		EncoderBaseXmlQt::serializeLong( "lowest", object->getTemperatureLowest(), doc, temperatureNode, encoder_options);
	return WRET_OK;
 }

WRESULT EncoderXml_Qt_Xtended_WeatherInfo::deserializeFrom( wosh::xtended::WeatherInfo* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	object->setTimestamp( EncoderBaseXmlQt::deserializeLong( "timestamp", 0, contNode ) );
	QDomElement periodNode = contNode.namedItem("period").toElement();
	if ( !periodNode.isNull() ) {
		object->setPeriodName( EncoderBaseXmlQt::deserializeString( "name", "", periodNode ) );
		object->setPeriodStart( EncoderBaseXmlQt::deserializeLong( "start", 0, periodNode ) );
		object->setPeriodEnd( EncoderBaseXmlQt::deserializeLong( "end", 0, periodNode ) );
	 }
	QDomElement conditionNode = contNode.namedItem("condition").toElement();
	if ( !conditionNode.isNull() ) {
		object->setConditionSummary( EncoderBaseXmlQt::deserializeString( "summary", "", conditionNode ) );
	 }
	QDomElement tempNode = contNode.namedItem("temperature").toElement();
	if ( !tempNode.isNull() ) {
		object->setTemperatureHighest( EncoderBaseXmlQt::deserializeLong( "highest", 0, tempNode ) );
		object->setTemperatureLowest( EncoderBaseXmlQt::deserializeLong( "lowest", 0, tempNode ) );
	 }
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


  }; // namespace qt
 }; // namespace persistence
}; // namespace wosh
